﻿#region 命名空间

using System;
using System.Linq;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using WinnerCMS.Common;
using WinnerCMS.BLL;
using WinnerCMS.Model.Attribute;
using WinnerCMS.Model.Enum;
using WinnerCMS.WebFactory;

#endregion

namespace WinnerCMS.WebUI.Admin.Category
{
    [Access("256001,256003")]
    public partial class Add : PageBase
    {
        private readonly CategoryManage _myBll = PolicyInjection.Create<CategoryManage>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var action = Request["Action"];
                if (action == "Edit")
                {
                    EditUiLoad();
                }
                else
                {
                    AddUiLoad();
                }
            }
        }

        private void AddUiLoad()
        {
            Title = "添加栏目";
            this.btnOK.Text = "添加";

            this._myBll.BindDDL(this.ddlCategory, BindOption.NoLink, "　　　", null);
            this._myBll.BindDDL(this.ddlCopyCategory, BindOption.NoLink, "<-无->", null);
            new ModelManage().BindDDL(this.ddlModel, "<请选择模型>");

            if (Request["ParentId"] != null)
            {
                this.ddlCategory.SelectedValue = Request["ParentId"];

                var model = CategoryManage.GetInfo(int.Parse(Request["ParentId"]));
                this.ddlModel.SelectedValue = model.ModelId.ToString();
                this.txtTemplateFileName.Text = model.TemplateFileName;
                this.txtShowPageTemplateFileName.Text = model.ShowPageTemplateFileName;
            }
        }

        private void EditUiLoad()
        {
            Title = "修改栏目";
            this.btnOK.Text = "修改";


            this._myBll.BindDDL(this.ddlCategory, BindOption.NoLink, "　　　", null);
            this._myBll.BindDDL(this.ddlCopyCategory, BindOption.NoLink, "<-无->", null);
            new ModelManage().BindDDL(this.ddlModel, "<请选择模型>");

            var model = CategoryManage.GetInfo(int.Parse(Request["ID"]));
            this.ddlCategory.SelectedValue = model.ParentId.ToString();
            this.ddlCategory.Enabled = false;
            this.ddlModel.SelectedValue = model.ModelId.ToString();
            this.txtCategoryName.Text = model.CategoryName;
            this.txtCategoryName_EN.Text = model.CategoryNameEN;
            this.txtTemplateFileName.Text = model.TemplateFileName;
            this.txtShowPageTemplateFileName.Text = model.ShowPageTemplateFileName;
            this.txtLinkUrl.Text = model.LinkUrl;


            this.txtFilePostfix.Text = model.FilePostfix;
            this.txtFilePrefix.Text = model.FilePrefix;
            this.RBL_URLType.SelectedValue = model.UrlStyle.ToString();
            this.txtCategoryFilePostfix.Text = model.CategoryFilePostfix;
            this.txtCategoryFilePrefix.Text = model.CategoryFilePrefix;
            this.rblCategoryURLType.SelectedValue = model.CategoryUrlStyle.ToString();
            this.txtCategoryIntro.Text = model.CategoryIntro;
            this.txtCategoryPic.Text = model.CategoryPic;
            this.txtCategoryDescription.Text = model.CategoryDescription;
            this.txtCategoryKeyWord.Text = model.CategoryKeyWord;
            this.RBL_EnableContribute.SelectedValue = model.EnableContribute.ToString();
            this.RBL_EnableComment.SelectedValue = model.EnableComment.ToString();


            this.CBL_Fields.DataTextField = "Title";
            this.CBL_Fields.DataValueField = "FieldName";
            this.CBL_Fields.DataSource = new FieldManage().GetShopModelField(byte.Parse(this.ddlModel.SelectedValue));
            this.CBL_Fields.DataBind();

            //加载被勾选的字段
            var list = new GoodsCustomFieldsManage().GetFieldsList(int.Parse(Request["ID"]));
            foreach (var g in list)
            {
                foreach (ListItem li in this.CBL_Fields.Items)
                {
                    if (li.Value == g.FieldName)
                        li.Selected = true;
                }
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            WinnerCMS.Model.Category model;

            if (Request["ID"] == null)
            {
                model = new WinnerCMS.Model.Category();
                model.Id = 0;
                model.ParentId = int.Parse(this.ddlCategory.SelectedValue);
            }
            else
            {
                model = CategoryManage.GetInfo(Convert.ToInt32(Request["ID"]));
            }

            model.ModelId = byte.Parse(this.ddlModel.SelectedValue);
            model.CategoryName = this.txtCategoryName.Text.Trim();
            model.CategoryNameEN = this.txtCategoryName_EN.Text.Trim();
            model.TemplateFileName = this.txtTemplateFileName.Text.Trim();
            model.ShowPageTemplateFileName = this.txtShowPageTemplateFileName.Text.Trim();
            model.LinkUrl = this.txtLinkUrl.Text.Trim();
            model.FilePostfix = this.txtFilePostfix.Text.Trim();
            model.FilePrefix = this.txtFilePrefix.Text.Trim();
            model.UrlStyle = int.Parse(this.RBL_URLType.SelectedValue);
            model.CategoryFilePostfix = this.txtCategoryFilePostfix.Text.Trim();
            model.CategoryFilePrefix = this.txtCategoryFilePrefix.Text.Trim();
            model.CategoryUrlStyle = int.Parse(this.rblCategoryURLType.SelectedValue);
            model.CategoryIntro = this.txtCategoryIntro.Text.Trim();
            model.CategoryPic = this.txtCategoryPic.Text.Trim();
            model.CategoryDescription = this.txtCategoryDescription.Text.Trim();
            model.CategoryKeyWord = this.txtCategoryKeyWord.Text.Trim();
            model.EnableContribute = bool.Parse(this.RBL_EnableContribute.SelectedValue);
            model.EnableComment = bool.Parse(this.RBL_EnableComment.SelectedValue);

            var fields = (from ListItem li in this.CBL_Fields.Items where li.Selected select li.Value).ToList();


            var resu = this._myBll.Save(model, fields);

            if (string.IsNullOrEmpty(resu.Errors) == false)
            {
                MsgBox.Message(resu.Errors, this);
            }
            else
            {
                Response.Redirect("Manage.aspx");
            }
        }

        protected void ddlCopyCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.CBL_Fields.DataSource = new FieldManage().GetShopModelField(byte.Parse(this.ddlModel.SelectedValue));
            this.CBL_Fields.DataTextField = "Title";
            this.CBL_Fields.DataValueField = "FieldName";
            this.CBL_Fields.DataBind();
        }
    }
}